﻿using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;

namespace CSC.Google
{
    /// <summary>
    /// A format string that gives information about the URL, and a list of arguments for that format string.
    /// </summary>
    [Serializable, DebuggerDisplay("{ToString()}")]
    public class FormatArgs
    {
        #region Private Variables
        [OptionalField, DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string format = null;
        [OptionalField, DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private Args[] args = null;
        #endregion

        /// <summary>
        /// A localized format string with $N placeholders, where N is the 1-indexed argument number. For example: 'Minifying the resource at URL $1 can save $2 bytes'.
        /// </summary>
        public string Format { get { return format; } }

        /// <summary>
        /// List of arguments for the format string.
        /// </summary>
        [DebuggerDisplay("Count = {Args.Length}")]
        public Args[] Args { get { return args; } }

        /// <summary>
        /// Combines the format string with the arguments
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(format);
            if (args != null && args.Length > 0)
                for (int i = 1; i <= args.Length; i++)
                    sb.Replace("$" + i, args[i - 1].Value);
            return sb.ToString();
        }
    }
}
